﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductManager.bl
{
    class BLProduct
    {
        public bool ValidateAdd(string name, string price, string quantity)
        {


            bool check = true;
            string error = "Error:" +
                            "\n============================\n";

            string validateName = ValidateName(name);
            if (!validateName.Equals(""))
            {
                check = false;
                error = error + validateName;
            }

            string validatePrice = ValidatePrice(price);
            if (!validatePrice.Equals(""))
            {
                check = false;
                error = error + validatePrice;
            }

            string validateQuantity = ValidateQuantity(quantity);
            if (!validateQuantity.Equals(""))
            {
                check = false;
                error = error + validateQuantity;
            }

            if (check)
            {
                return true;
            }
            else
            {
                System.Windows.Forms.MessageBox.Show(error, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return false;
            }
        }


        public bool ValidateSearchByName(string name)
        {


            bool check = true;
            string error = "Error:" +
                            "\n============================\n";

            string validateName = ValidateName(name);
            if (!validateName.Equals(""))
            {
                check = false;
                error = error + validateName;
            }

            if (check)
            {
                return true;
            }
            else
            {
                System.Windows.Forms.MessageBox.Show(error, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return false;
            }
        }

        public bool ValidateSearchByPrice(string fPrice, string tPrice)
        {


            bool check = true;
            string error = "Error:" +
                            "\n============================\n";

            string validateFPrice = ValidatePrice(fPrice);
            if (!validateFPrice.Equals(""))
            {
                check = false;
                error = error + validateFPrice;
            }

            string validateTPrice = ValidatePrice(tPrice);
            if (!validateTPrice.Equals(""))
            {
                check = false;
                error = error + validateTPrice;
            }

            if (check)
            {
                return true;
            }
            else
            {
                System.Windows.Forms.MessageBox.Show(error, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return false;
            }
        }


        public string ValidateName(string name)
        {
            string error = "";

            if ("".Equals(name))
            {
                error += "\n+ Name can't empty.";
            }

            return error;
        }


        public string ValidatePrice(string price)
        {
            string error = "";
            try
            {
                decimal pri = Convert.ToDecimal(price);
                if (pri < 0)
                {
                    error += "\n+ Price must be greater than 0.";
                }
            }
            catch (Exception ex)
            {
                error += "\n+ Price invalid.";
            }

            return error;
        }

        public string ValidateQuantity(string quantity)
        {
            string error = "";
            try
            {
                int quant = Convert.ToInt32(quantity);
                if (quant < 0)
                {
                    error += "\n+ Quantity must be greater than 0.";
                }
            }
            catch (Exception ex)
            {
                error += "\n+ Quantity invalid.";
            }
            return error;
        }
    }
}
